package com.sx.micowell.module.sport.end

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.bar.OnTitleBarListener
import com.example.mvvm.base.ContainerActivity
import com.example.mvvm.base.data.DataBindingConfig
import com.github.mikephil.charting.data.BarEntry
import com.gyf.immersionbar.ImmersionBar
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragSportEndBinding
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.SportEndModel
import com.sx.micowell.framework.view.chartManager.initDataChart
import com.sx.micowell.framework.view.chartManager.initUIChart
import retrofit2.http.Field
import java.util.*

/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/9/14 16:10
 */
@Route(path = RoutePath.SPORT_END)
class SportEndFragment : MyFragment<FragSportEndBinding, ContainerActivity>(), OnTitleBarListener {

    @Autowired(name = IntentKey.CALORIES)
    lateinit var CALORIES: String

    @Autowired(name = IntentKey.TIME)
    lateinit var TIME: String

    private val viewModel: SportEndModel by lazy {
        getActivityScopeViewModel(SportEndModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig =
        DataBindingConfig(R.layout.frag_sport_end, BR.viewModel, viewModel)

    override fun initView() {
        ImmersionBar.with(this).titleBar(binding.title).statusBarDarkFont(true).init()
        binding.title.setOnTitleBarListener(this)
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.lastWeekList.observe(this, { data ->
            var listCalorie =
                data.map { BarEntry(data.indexOf(it).toFloat(), it.calorie) }.toList()
            var listTime = data.map { it.createTime }.toList().toTypedArray()
            binding.historyLine.initDataChart(listCalorie,listTime)
        })

    }

    override fun initData() {
        binding.historyLine.initUIChart()
        viewModel.calories.value = CALORIES
        viewModel.time.value = TIME
        viewModel.getSportEnd()
        viewModel.getSportLastWeek()
    }

    override fun onLeftClick(view: View?) {
        myActivity.finish()
    }

    override fun onTitleClick(view: View?) {

    }

    override fun onRightClick(view: View?) {
    }

}